/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.facilscrum.form.userstories;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import org.facilscrum.beans.Equiposusuarios;
import org.javscaz.javscaztools.utilidades.Utilidades;
import org.facilscrum.beans.Proyectos;
import org.facilscrum.beans.Userstories;
import org.facilscrum.beans.Usuarios;
import org.facilscrum.controller.EquiposusuariosController;
import org.facilscrum.controller.ProyectosController;
import org.facilscrum.controller.UserstoriesController;
import org.facilscrum.controller.UsuariosController;
import org.facilscrum.form.usuarios.UsuariosForm;
import org.facilscrum.generales.Mensajeria;
import org.facilscrum.sesion.MySesion;

/**
 *
 * @author avbravo
 */
public class UserstoriesUpdateForm extends javax.swing.JInternalFrame {
 EquiposusuariosController equiposusuariosController = new EquiposusuariosController();
    ProyectosController proyectosController = new ProyectosController();
    UserstoriesController userstoriesController = new UserstoriesController();
    Userstories userstories = new Userstories();
    UsuariosController usuariosController = new UsuariosController();
    Usuarios usuarios = new Usuarios();
    DefaultTableModel modelo;

    /**
     * Creates new form BacklogForm
     */
    public UserstoriesUpdateForm() {
        initComponents();
        modelo = (DefaultTableModel) jTable1.getModel();
        jTable1.setEnabled(true);
        jButtonGuardar.setEnabled(false);
        if (!cargarComboUsuarios()) {
            jComboBoxUsuarios.setEditable(false);
            jTable1.setEnabled(false);
        }
        if (!MySesion.getSeleccionoProyectoTrabajo()) {
            Mensajeria.MensajeWarning("Debe indicar el proyecto de trabajo");
        } else {
            if (cargarComboUsuarios()) {
                jButtonGuardar.setEnabled(true);
            }
            jTextFieldProyecto.setText(MySesion.getProyectos().getProyecto());
        }
        cargarUserStories();
        limpiar();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldId = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldNombre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldImportancia = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButtonGuardar = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaComoProbarlo = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaNotas = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jComboBoxUsuarios = new javax.swing.JComboBox();
        jComboBoxPuntos = new javax.swing.JComboBox();
        jTextFieldProyecto = new javax.swing.JTextField();
        jButtonImprimir = new javax.swing.JButton();

        setClosable(true);
        setTitle("Actualizar User stories");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Proyecto");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "nombre", "importancia", "puntos", "usuario"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(60);
        jTable1.getColumnModel().getColumn(2).setMaxWidth(100);
        jTable1.getColumnModel().getColumn(3).setMaxWidth(60);

        jLabel2.setText("Id");

        jTextFieldId.setEnabled(false);

        jLabel3.setText("Nombre");

        jLabel4.setText("Importancia");

        jLabel5.setText("Puntos");

        jButtonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/save_icon.gif"))); // NOI18N
        jButtonGuardar.setToolTipText("Guardar");
        jButtonGuardar.setEnabled(false);
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/return.gif"))); // NOI18N
        jButtonCerrar.setToolTipText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/clear.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/delete.gif"))); // NOI18N
        jButtonEliminar.setToolTipText("Eliminar");
        jButtonEliminar.setEnabled(false);
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jLabel6.setText("Como probarlo");

        jTextAreaComoProbarlo.setColumns(20);
        jTextAreaComoProbarlo.setRows(5);
        jScrollPane2.setViewportView(jTextAreaComoProbarlo);

        jLabel7.setText("Notas");

        jTextAreaNotas.setColumns(20);
        jTextAreaNotas.setRows(5);
        jScrollPane3.setViewportView(jTextAreaNotas);

        jLabel9.setText("Usuario");

        jComboBoxPuntos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "8", "13", "20", "40", "100" }));

        jTextFieldProyecto.setEnabled(false);

        jButtonImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/print.png"))); // NOI18N
        jButtonImprimir.setToolTipText("Eliminar");
        jButtonImprimir.setEnabled(false);
        jButtonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldProyecto, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(400, 521, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel9)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jTextFieldImportancia, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel5)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jComboBoxPuntos, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                                .addComponent(jScrollPane3))
                            .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(127, 127, 127)
                .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldProyecto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(7, 7, 7)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldImportancia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jComboBoxPuntos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(7, 7, 7)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBoxUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonCerrar, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonEliminar, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonLimpiar)
                            .addComponent(jButtonGuardar)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButtonImprimir)))
                .addGap(46, 46, 46))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        try {
            Integer fila = jTable1.getSelectedRow();
            if (fila == -1) {
                Mensajeria.Mensaje("No se selecciono ninguna fila");

                return;
            }
            userstories.setIduserstories(Integer.parseInt(jTable1.getValueAt(fila, 0).toString()));
            Userstories us = userstoriesController.findByPrimaryKey(userstories);
            if (userstories != null) {
                userstories = us;
            } else {
                Mensajeria.MensajeWarning("No existe un userstories con ese codigo");
                limpiar();
                return;
            }
            jTextFieldId.setText(userstories.getIduserstories().toString());
            jTextFieldNombre.setText(userstories.getNombre());
            jTextFieldImportancia.setText(userstories.getImportancia().toString());
            jComboBoxPuntos.setSelectedItem(userstories.getPuntos().toString());
            jTextAreaComoProbarlo.setText(userstories.getComoprobarlo());
            jTextAreaNotas.setText(userstories.getNotas());


           

            Usuarios u = new Usuarios();
            u.setUsername(userstories.getUsername());
            u = usuariosController.findByPrimaryKey(u);

            if (u != null) {                
                jComboBoxUsuarios.setSelectedItem(u.getNombre() + " - " + u.getCedula());
            } else {
                Mensajeria.Mensaje("No existe un usuario con ese username" + userstories.getIdproyecto());

            }
            jButtonGuardar.setEnabled(true);
            jButtonEliminar.setEnabled(true);
              jButtonImprimir.setEnabled(true);
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "MouseClicked()");
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed
        actualizar();
    }//GEN-LAST:event_jButtonGuardarActionPerformed

    private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed
        limpiar();
    }//GEN-LAST:event_jButtonLimpiarActionPerformed

    private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
        eliminar();
    }//GEN-LAST:event_jButtonEliminarActionPerformed

    private void jButtonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirActionPerformed
HashMap map = new HashMap();
        List<Userstories> list = new ArrayList();
        list.add(userstories);
        try {
            imprimir(list, map);
        } catch (JRException ex) {
            Logger.getLogger(UsuariosForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButtonImprimirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JComboBox jComboBoxPuntos;
    private javax.swing.JComboBox jComboBoxUsuarios;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextAreaComoProbarlo;
    private javax.swing.JTextArea jTextAreaNotas;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldImportancia;
    private javax.swing.JTextField jTextFieldNombre;
    private javax.swing.JTextField jTextFieldProyecto;
    // End of variables declaration//GEN-END:variables

   private Boolean cargarComboUsuarios() {
        try {

            jComboBoxUsuarios.removeAllItems();
            String sql = "idequipo='" + MySesion.getProyectos().getIdequipo() + "'";
            if (equiposusuariosController.query(sql)) {
                for (Equiposusuarios eu : equiposusuariosController.getEquiposusuarioslist()) {
                                     jComboBoxUsuarios.addItem(eu.getUsuarios().getNombre() + " - " + eu.getUsuarios().getCedula());
                }
                return true;
            } else {
                Mensajeria.MensajeWarning("No hay usuarios asignados al equipo de este proyecto");
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "cargarComboUsuarios() ");
        }
        return false;
    }
    /*
     * 
     */

    private Boolean guiToData() {
        try {

            if (jTextFieldId.getText() == null || jTextFieldId.getText().equals("")) {
                Mensajeria.Mensaje("El id no debe estar vacio");
                return false;
            }
            userstories.setIduserstories(Integer.parseInt(jTextFieldId.getText()));
            if (jTextFieldNombre.getText() == null || jTextFieldNombre.getText().equals("")) {
                Mensajeria.Mensaje("El nombre  no debe estar vacio");
                return false;
            }
            if (jTextFieldImportancia.getText() == null || jTextFieldImportancia.getText().equals("")) {
                Mensajeria.Mensaje("La importancia no debe estar vacio");
                return false;
            }


            if (Integer.parseInt(jTextFieldImportancia.getText()) <= 0) {
                Mensajeria.MensajeWarning("La importancia debe ser mayor de cero");
                return false;
            }

            if (jTextAreaComoProbarlo.getText() == null || jTextAreaComoProbarlo.getText().equals("")) {
                Mensajeria.Mensaje("Como probarlo  no debe estar vacio");
                return false;
            }
            if (jTextAreaNotas.getText() == null || jTextAreaNotas.getText().equals("")) {
                Mensajeria.Mensaje("Notas  no debe estar vacio");
                return false;
            }
            return true;
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "actualizar()");

        }
        return false;
    }

    private void actualizar() {
        try {
            if (!guiToData()) {
                return;
            }
            userstories = userstoriesController.findByPrimaryKey(userstories);
            userstories.setNombre(jTextFieldNombre.getText());
            userstories.setImportancia(Integer.parseInt(jTextFieldImportancia.getText()));
            userstories.setNotas(jTextAreaNotas.getText());
            userstories.setComoprobarlo(jTextAreaComoProbarlo.getText());           

            Integer k = jComboBoxUsuarios.getSelectedIndex();
            if (k != -1) {
                //  Usuarios usuarios = usuariosController.getUsuarioslist().get(k);
                Equiposusuarios e = equiposusuariosController.getEquiposusuarioslist().get(k);
                userstories.setUsername(e.getUsername());
            } else {
                Mensajeria.Mensaje("No se ha seleccionado ningun usuario");
                return ;
            }
            int m = jComboBoxPuntos.getSelectedIndex();
            if (m != -1) {
                userstories.setPuntos(Integer.parseInt(jComboBoxPuntos.getSelectedItem().toString()));
            } else {
                Mensajeria.Mensaje("No se ha seleccionado los puntos");
                return;
            }

            if (userstoriesController.update(userstories)) {
                Mensajeria.Mensaje("Se actualizo el user stories");

                limpiar();
                cargarUserStories();
            }


        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "actualizar()");

        }
    }

    private void eliminar() {
        try {
            if (!guiToData()) {
                return;
            }
            if (Mensajeria.PreguntaSiNo("Desea eliminar user stories", "Eliminar").equals("no")) {
                return;
            }
            if (userstoriesController.delete(userstories)) {
                Mensajeria.Mensaje("Se elimino user stories");
                limpiar();
                cargarUserStories();
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "eliminar()");
        }
    }

    private void cargarUserStories() {
        try {
            Proyectos proyectos = MySesion.getProyectos();
            modelo = Utilidades.limpiarModelo(modelo);
            if (userstoriesController.query("asignado='no' and idproyecto='" + proyectos.getIdproyecto() + "' order by importancia desc")) {
                for (Userstories u : userstoriesController.getUserstorieslist()) {
                    Object[] filas = new Object[5]; // Hay tres columnas en la tabla       
                    filas[0] = u.getIduserstories();
                    filas[1] = u.getNombre();
                    filas[2] = u.getImportancia();
                    filas[3] = u.getPuntos();
                    filas[4] = u.getUsuarios().getNombre();
                    modelo.addRow(filas);
                }
            }


        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "cargarUserStories()");

        }
    }

    private void limpiar() {
        jTextFieldId.setText("");
        jTextFieldNombre.setText("");
        jTextFieldImportancia.setText("");
        jTextAreaComoProbarlo.setText("");
        jTextAreaNotas.setText("");
        jButtonGuardar.setEnabled(false);
        jButtonEliminar.setEnabled(false);
          jButtonImprimir.setEnabled(false);
    }
    
    private void imprimir(List<Userstories> aux, HashMap map) throws JRException {
        try {
            JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(aux);
//Indicamos la ruta del reporte
            InputStream reporte = (InputStream) getClass().getResourceAsStream("/org/facilscrum/reportes/userstories.jasper");
            if (reporte == null) {
                Mensajeria.Mensaje("No se pudo cargar el reporte");
            } else {
                JasperPrint print = JasperFillManager.fillReport(reporte, map, ds);
                JasperViewer.viewReport(print, false);
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "Imprimir()");
        }
    }
}
